package com.haocang.szgm.ui.monitor.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.esri.android.map.Callout;
import com.esri.android.map.event.OnSingleTapListener;
import com.esri.core.geometry.Point;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.PictureMarkerSymbol;
import com.haocang.szgm.R;
import com.haocang.szgm.activity.comm.CommMapFinalActivity;
import com.haocang.szgm.requestcenter.DataModel;
import com.haocang.szgm.ui.analysis.activity.DataAnalysisActivity;
import com.haocang.szgm.ui.monitor.adapter.MapDataAdapter;
import com.haocang.szgm.ui.monitor.adapter.MapLegendAdapter;
import com.haocang.szgm.ui.monitor.bean.MapDataBean;
import com.haocang.szgm.ui.monitor.bean.MapLegendBean;
import com.haocang.szgm.ui.monitor.bean.RealDataBean;
import com.haocang.szgm.ui.monitor.bean.ShowDataBean;
import com.haocang.szgm.utils.config.Config;
import com.haocang.szgm.utils.config.WebApi;
import com.haocang.commonlib.gismap.MapUtils;
import com.haocang.commonlib.network.interfaces.InitDataBeanBack;
import com.haocang.commonlib.recyclerView.BaseAdapter;

import net.tsz.afinal.annotation.view.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ComprehensiveActivity extends CommMapFinalActivity {


    private Context context;


    private List<MapDataBean> MapData_List = new ArrayList<>();

    @ViewInject(id = R.id.tv_map_lable)
    LinearLayout tv_map_lable;
    @ViewInject(id = R.id.recyclerview)
    RecyclerView recyclerview;

    @ViewInject(id = R.id.tv_map_legend)
    LinearLayout tv_map_legend;
    @ViewInject(id = R.id.lin_legend)
    LinearLayout lin_legend;

    @ViewInject(id = R.id.tv_close)
    TextView tv_close;

    private MapDataAdapter mapDataAdapter;
    private List<MapLegendBean> mapLegendBeanList = new ArrayList<>();
    private MapLegendAdapter mapLegendAdapter;
    private Map<Integer, MapDataBean> mLocationMapList = new HashMap<>();
    String ls_legend = "ALL";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        initView();

        initEvent();

        initData();
        initMyLocation();

    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_comprehensive;
    }

    @Override
    protected boolean showNavigation() {
        return true;
    }

    @Override
    protected boolean showMapPipe() {
        return false;
    }

    @Override
    protected boolean showMedia() {
        return false;
    }

    @Override
    protected int showMoreIcon() {
        return 0;
    }

    @Override
    protected String showTitleName() {
        return "综合监测";
    }

    private void initView() {
        IsLable = false;

        mapLegendAdapter = new MapLegendAdapter(context);
        recyclerview.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL) {
            @Override
            public boolean canScrollVertically() {
                return true;//是否允许滑动
            }
        });
        recyclerview.setAdapter(mapLegendAdapter);
    }


    private void initEvent() {

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        });


        tv_map_lable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (IsLable) {
                    IsLable = false;
                } else {
                    IsLable = true;
                }
                AddMonitorPoints(MapData_List, ls_legend);

            }
        });

        tv_map_legend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lin_legend.getVisibility() == View.VISIBLE) {
                    lin_legend.setVisibility(View.GONE);
                } else {
                    lin_legend.setVisibility(View.VISIBLE);
                }
            }
        });

        tv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lin_legend.setVisibility(View.GONE);
            }
        });


        mapLegendAdapter.setOnItemChildClickListener(new BaseAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseAdapter adapter, View v, int position) {
                MapLegendBean model = mapLegendAdapter.getItem(position);

                List<MapLegendBean> smapLegendBeanList = new ArrayList<>();
                ls_legend = "";
                for (MapLegendBean m : mapLegendBeanList) {
                    if (m.getType() == model.getType()) {
                        m.setChecked(!m.getChecked());
                    }
                    smapLegendBeanList.add(m);

                    if (m.getChecked()) {
                        ls_legend += m.getType();
                    }
                }
                mapLegendBeanList = smapLegendBeanList;
                mapLegendAdapter.setNewData(mapLegendBeanList);
                // AddMonitorPoints(MapData_List, ls_legend);

                initData();
            }
        });


        mMapView.setOnSingleTapListener(new OnSingleTapListener() {
            @Override
            public void onSingleTap(float x, float y) {

                int[] ids = gLayerPos.getGraphicIDs(x, y, 5);
                if (ids.length > 0) {

                    final MapDataBean m_model = mLocationMapList.get(ids[0]);

                    if (m_model != null) {


                        //初始化显示气泡
                        final Callout mCallout = mMapView.getCallout();
                        LayoutInflater inflater = LayoutInflater.from(context);
                        View view = inflater.inflate(R.layout.callout_screen_info, null);
                        TextView tv_title = view.findViewById(R.id.tv_name);
                        LinearLayout iv_close = view.findViewById(R.id.iv_close);
                        LinearLayout data_lin = view.findViewById(R.id.data_lin);
                        RecyclerView rv_recyclerview = view.findViewById(R.id.rv_recyclerview);//数据列表

                        mapDataAdapter = new MapDataAdapter(context);
                        rv_recyclerview.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL) {
                            @Override
                            public boolean canScrollVertically() {
                                return true;//是否允许滑动
                            }
                        });
                        rv_recyclerview.setAdapter(mapDataAdapter);

                        initMpointData(mapDataAdapter, m_model.getID());

                        tv_title.setText(m_model.getName());
                        mCallout.setOffset(0, 15);
                        mCallout.show(mMapView.toMapPoint(new Point(x, y)), view);
                        mCallout.setStyle(R.xml.callout_style_02);

                        iv_close.setOnClickListener(new View.OnClickListener() {
                                                        @Override
                                                        public void onClick(View v) {
                                                            mCallout.hide();
                                                        }
                                                    }
                        );

                        mapDataAdapter.setOnItemChildClickListener(new BaseAdapter.OnItemChildClickListener() {
                            @Override
                            public void onItemChildClick(BaseAdapter adapter, View v, int position) {
                                RealDataBean s_model = mapDataAdapter.getItem(position);
                                Intent it = new Intent(context, DataAnalysisActivity.class);
                                it.putExtra("FromHome", false);
                                it.putExtra("MPOINT", s_model.getMpointID());
                                startActivity(it);
                            }
                        });

                    }

                }
            }
        });

    }

    private void initData() {

        String ApiUrl = Config.host + WebApi.Api_GetMonitorData;

        DataModel.GetApiRequestMode("MapDataBean", ApiUrl, new InitDataBeanBack<MapDataBean>() {
            @Override
            public void callbak(boolean b, List<MapDataBean> list, String msg) {
                MapData_List = list;
                int li_a = 0, li_b = 0, li_c = 0, li_d = 0, li_e = 0, li_bz = 0, li_f = 0;
                for (MapDataBean model : MapData_List) {
                    switch (model.getType()) {
                        case "WSC"://污水厂
                            li_a++;
                            break;
                        case "BZ"://污水厂
                            li_bz++;
                            break;
                        case "GWJC_GWYW"://管网液位
                            li_b++;
                            break;
                        case "GWJC_GWSZ"://管网水质
                            li_c++;
                            break;
                        case "GWJC_GWLL"://管网流量
                            li_d++;
                            break;
                        case "JSC"://积水尺
                            li_e++;
//                            Log.e("JSC",model.toString());
                            break;
                        case "ZGYL"://专管压力
                            li_f++;
//                            Log.e("ZGYL",model.toString());
                            break;
                    }
                }

                if (mapLegendBeanList.size() > 0) {
                    mapLegendBeanList.get(0).setNum(li_a);
                    mapLegendBeanList.get(1).setNum(li_bz);
                    mapLegendBeanList.get(2).setNum(li_b);
                    mapLegendBeanList.get(3).setNum(li_c);
                    mapLegendBeanList.get(4).setNum(li_d);
                    mapLegendBeanList.get(5).setNum(li_e);
                    mapLegendBeanList.get(6).setNum(li_f);
                } else {
                    mapLegendBeanList.add(new MapLegendBean(R.mipmap.map_wsc_g, "污水厂", li_a, true, "WSC"));
                    mapLegendBeanList.add(new MapLegendBean(R.mipmap.map_bz_g, "泵站", li_bz, true, "BZ"));
                    mapLegendBeanList.add(new MapLegendBean(R.mipmap.map_wsgw_g, "管网液位", li_b, true, "GWJC_GWYW"));
                    mapLegendBeanList.add(new MapLegendBean(R.mipmap.map_gwsz_g, "管网水质", li_c, true, "GWJC_GWSZ"));
                    mapLegendBeanList.add(new MapLegendBean(R.mipmap.map_gwll_g, "管网流量", li_d, true, "GWJC_GWLL"));
                    mapLegendBeanList.add(new MapLegendBean(R.mipmap.map_jsc_g, "积水尺", li_e, true, "JSC"));
                    mapLegendBeanList.add(new MapLegendBean(R.mipmap.map_zgyl_g, "专管压力", li_f, true, "ZGYL"));
                    mapLegendAdapter.setNewData(mapLegendBeanList);
                }
                AddMonitorPoints(MapData_List, ls_legend);
            }
        });

    }


    //请求弹框数据
    private void initMpointData(final MapDataAdapter ma, String Fcat_ID) {
        String url = Config.host + WebApi.Api_GetByFcatIdMpointList + Fcat_ID;
        Log.d("测试数据", "返回数据" + url);
        DataModel.GetApiRequestMode("RealDataBean", url, new InitDataBeanBack<RealDataBean>() {
            @Override
            public void callbak(boolean b, List<RealDataBean> list, String msg) {
                ma.setNewData(list);
            }
        });

    }


    //添加一些点
    private void AddMonitorPoints(List<MapDataBean> List, String Legends) {

        gLayerPos.removeAll();
        mLocationMapList.clear();


        //添加位置点
        for (MapDataBean model : List) {

            if (!Legends.contains(model.getType()) && !Legends.equals("ALL")) continue;

            Point mapPoint = new Point(model.getX(), model.getY());
            int icon = GetIcon(model.getType(), model.getState());
            if (icon == 0) {
                continue;
            }
            PictureMarkerSymbol locationSymbol = new PictureMarkerSymbol(mContext.getResources().getDrawable(icon));
            Graphic graphic = new Graphic(mapPoint, locationSymbol);

            if (IsLable) {
                PictureMarkerSymbol markerSymbol = new PictureMarkerSymbol(MapUtils.createMapBitMap(context, model.getName()));
                Graphic graphictext = new Graphic(mapPoint, markerSymbol);
                markerSymbol.setOffsetY(-23);
                // Graphic graphic_start_p0 = new Graphic(startPoint,new SimpleMarkerSymbol(start_color,19, SimpleMarkerSymbol.STYLE.CIRCLE));
                mLocationMapList.put(gLayerPos.addGraphic(graphictext), model);
            }
            mLocationMapList.put(gLayerPos.addGraphic(graphic), model);

        }
    }


    //退出程序
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            return super.onKeyDown(keyCode, event);
        } else {
            return false;
        }
    }


}
